/*************************************************************************
	> File Name: acqdata.cpp
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年03月13日 星期二 15时15分52秒
 ************************************************************************/

#include <stdio.h>
#include <time.h>
#include <utils/custom.h>
#include "acqdata.h"

AcqData::AcqData()
{
    type = TYPE_NONE;
    itemid = 0;
    flag = 0;
    data.f64 = 0;
}

AcqData::AcqData(const AcqItem &item_)
{
    flag = 0;
    type = item_.data.type;
    itemid = item_.itemid;
    copydata(type,item_.data.data);
}

AcqData::AcqData(const OpValue &data_) {
    flag = 0;
    type = data_.type;
    copydata(data_.type,data_.data);
}

AcqData &AcqData::operator =(const AcqItem &item_)
{
    flag = 0;
    type = item_.data.type;
    itemid = item_.itemid;
    copydata(type,item_.data.data);
    return *this;
}

AcqData &AcqData::operator =(const OpValue &data_)
{
    flag = 0;
    type = data_.type;
    copydata(data_.type,data_.data);
    return *this;
}

void AcqData::SetCommit(int index)
{
    flag |= 0x01 <<index;
}

void AcqData::SetVerify(int index)
{
    flag |= 0x10 <<index;
}

void AcqData::ResetFlag(int index)
{
    flag &= ~(0x11<<index);
}

void AcqData::SetTime(const time_t &t)
{
    acqtime = t;
}

void AcqData::SetId(U16 id)
{
    itemid = id;
}

time_t AcqData::GetTime()
{
    return acqtime;
}

U8 AcqData::GetFlag()
{
    return flag;
}

bool AcqData::IsCommit(int index)
{
    return (flag & (0x01<<index));
}

bool AcqData::IsVerify(int index)
{
    return (flag &(0x10<<index));
}

bool AcqData::IsAllVerify(int num)
{
    for (int i = 0;i < num;i++) {
        U8 f = 0x11<<i;
        if (!(f & flag))
            return FALSE;
    }
    return TRUE;
}

Json::Value AcqData::ToJson()
{
    switch(type) {
    case TYPE_S8:
        return data.s8;
    case TYPE_U8:
        return data.u8;
    case TYPE_S16:
        return data.s16;
    case TYPE_U16:
        return data.u16;
    case TYPE_S32:
        return data.s32;
    case TYPE_U32:
        return data.u32;
    case TYPE_S64:
        return data.s64;
    case TYPE_U64:
        return data.u64;
    case TYPE_F32:
        return data.f32;
    case TYPE_F64:
        return data.f64;
    default:
        return data.f64;
    }
}

void AcqData::copydata(TYPE_DEFINE type,const OpValue::Udata &udata)
{
    switch (type) {
    case TYPE_U8:
        data.u8 = udata.u8;
        break;
    case TYPE_S8:
        data.s8 = udata.s8;
        break;
    case TYPE_U16:
        data.u16 = udata.u16;
        break;
    case TYPE_S16:
        data.s16 = udata.s16;
        break;
    case TYPE_U32:
        data.u32 = udata.u32;
        break;
    case TYPE_S32:
        data.s32 = udata.s32;
        break;
    case TYPE_U64:
        data.u64 = udata.u64;
        break;
    case TYPE_S64:
        data.s64 = udata.s64;
        break;
    case TYPE_F32:
        data.f32 = udata.f32;
        break;
    case TYPE_F64:
        data.f64 = udata.f64;
        break;
    default:
        LOG_ERR("type error");
        break;
    }
}

void AcqData::copydata(TYPE_DEFINE type, const Udata &udata)
{
    switch (type) {
    case TYPE_U8:
        data.u8 = udata.u8;
        break;
    case TYPE_S8:
        data.s8 = udata.s8;
        break;
    case TYPE_U16:
        data.u16 = udata.u16;
        break;
    case TYPE_S16:
        data.s16 = udata.s16;
        break;
    case TYPE_U32:
        data.u32 = udata.u32;
        break;
    case TYPE_S32:
        data.s32 = udata.s32;
        break;
    case TYPE_U64:
        data.u64 = udata.u64;
        break;
    case TYPE_S64:
        data.s64 = udata.s64;
        break;
    case TYPE_F32:
        data.f32 = udata.f32;
        break;
    case TYPE_F64:
        data.f64 = udata.f64;
        break;
    default:
        LOG_ERR("type error");
        break;
    }
}
